package com.xxx.car.web.api;

import com.xxx.car.entity.vo.request.ReqChargeCreateOrder;
import com.xxx.car.entity.vo.request.ReqChargeDetailQuery;
import com.xxx.car.entity.vo.response.RespChargeDetail;
import com.xxx.car.entity.vo.response.RespChargeGive;
import com.xxx.car.service.TradeService;
import com.xxx.car.utils.UUIDUtils;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import com.xxx.common.util.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by xuwenjie on 2017/6/16.
 */
@Controller
@RequestMapping("/api/charge")
public class ApiChargeController {

    @Autowired
    private TradeService tradeService;

    @RequestMapping("/list")
    @ResponseBody
    public APIResponse goodsList() {
        List<RespChargeGive> giveList = tradeService.getAllChargeGive();
        return APIResponse.returnSuccess(giveList);
    }

    @RequestMapping("/createOrder")
    @ResponseBody
    public APIResponse createOrder(@Valid @ModelAttribute ReqChargeCreateOrder reqChargeCreateOrder, HttpServletRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        reqChargeCreateOrder.setUserIp(IPUtils.getIpAddr(request));
        Map<String, String> payMap = tradeService.createPayOrder(reqChargeCreateOrder);
        return APIResponse.returnSuccess(payMap);
    }

    @RequestMapping("/createOrderForTest")
    @ResponseBody
    public APIResponse createOrderForTest(@Valid @ModelAttribute ReqChargeCreateOrder reqChargeCreateOrder, HttpServletRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        reqChargeCreateOrder.setUserIp(IPUtils.getIpAddr(request));
        String orderId = UUIDUtils.createId();
        try {
            tradeService.createPayOrder(orderId, reqChargeCreateOrder);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tradeService.finishPay(orderId, "TEST_3RD_ID", reqChargeCreateOrder.getAmt());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/detail")
    @ResponseBody
    public APIResponse chargeDetail(@Valid @ModelAttribute ReqChargeDetailQuery reqChargeDetailQuery, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        List<RespChargeDetail> chargeDetails = tradeService.findChargeDetail(reqChargeDetailQuery);
        return APIResponse.returnSuccess(chargeDetails);
    }


}
